' js plugin for maxedit host

Strict

Import "../maxtk/maxtk.bmx"
'Import axe.maxedit

Import "cplugin.bmx"

Global JSKeywords$[]=["break","for","new","true","with","continue","function",..
"null","typeof","else","if","return","var","false","in","this","while",..
"abstract","default","implements","static","void","boolean","do","import",..
"super","byte","double","instanceof","switch","case","extends","int",..
"synchronized","catch","final","interface","throw","char","finally",..
"long","throws","class","float","native","transient","const","goto",..
"package","try"]

Global JSExtensions$[]=["js"]

Type TJSPlugin Extends TCPlugin

	Method New()
		name="JScript"
		keywords=JSKeywords
		extensions=JSExtensions
	End Method

	Method Open:Object(path$)
		Return TJSCode.Open(Self,path)
	End Method
	
End Type


Type TJSCode Extends TCCode

	Function Open:TJSCode(plugin:TCPlugin,path$)
		Local	code:TJSCode
		Local	stream:TStream		

		If path
			stream=ReadFile(path)
			If Not stream 
				Notify "Open could not read from "+path
				Return Null
			EndIf
			CloseStream stream
		Else
			Throw "temp cpp files not allowed"
		EndIf

		code=New TJSCode
'		code.isbmx=True
		code.cppplugin=plugin
		code.host=plugin.host		
		code.options=plugin.options

		code.active=True
		code.path=path

		plugin.host.addpanel(code)		
		code.textarea=CreateTextArea(0,0,ClientWidth(code.panel),ClientHeight(code.panel),code.panel,0)
		SetGadgetFilter code.textarea,code.FilterKey,code
		SetTextAreaText code.textarea,"~n"
		SetGadgetLayout code.textarea,1,1,1,1
		code.Refresh
		code.SetDirty False
		plugin.host.UpdateProgress "Reading Stream"
		code.ReadSource(path)
		code.UpdateCode
		code.undolist.clear
' cludge first dirty		
		code.filesrc=TextAreaText(code.textarea)
		code.SetDirty False
		Return code
	End Function


	Method GetNode:TDataNode()
		Local	root:TCodeNode,n:TCodeNode
		Local	src$,line,col
		Local	p,p1,r,f,e
		
		n=New TCodeNode
		n.name=StripDir(path)
		n.owner=Self
		n.count=src.length
		root=n
'update rem,function
		src=cleansrcl
		p1=src.length
		p=-1;r=-1;f=-1;
		While p<p1			'src.length
			While r<=p
				r=src.Find("/*",r+1)	'FindToken("Rem",src,r+1)
				If r=-1 r=src.length
			Wend
			While f<=p
				f=src.Find("function ",f+1)	'FindToken("Function",src,f+1)
				If f=-1 f=src.length
			Wend
			If r<f
				p=src.find("*/",r+1)			'FindEndToken("Rem",src,r+1,True)
				If p=-1 p=src.length
				Continue
			EndIf
			If f=src.length Exit		
			e=src.find(EOL,f)
			Local func$=cleansrc[f+9..e]
			If Len(func) And Asc(func)<96		
				Local g=func.Find("{")
				If g>1 func=func[..g]
				n.AddCodeNode(func,f,e)
			EndIf
			p=f+1
		Wend

		If codenode
			codenode.Sync(root)	
			root.Free
		Else
			codenode=root
		EndIf
		Return codenode
	End Method

End Type
